(function () {
	etlinking.views.groupList = new GroupListFunc();

	function GroupListFunc () {
		var _self = this;

		_self.init = init;

		var cssClass = {
			selected:"selected"
		};
		var _groupType;
		var socialActions = nsbase.config.actionUrl.social,
			utilities = nsbase.utility,
			validations = nsbase.utility.validations,
        	clientResource = nsbase.resources.clientResource;

		function init(groupType){
			_groupType = groupType;
			var $groupTitle = $("#groupTitle");
			if(_groupType == 1){
				$groupTitle.text(clientResource.lblMyGroup);
			}
			if(_groupType == 2){
				$groupTitle.text(clientResource.lblAllGroup);
			}
      		$(".nav_menu li.Social").addClass(cssClass.selected);
			initGrid();
		}

		function initGrid () {
			var gridConfig = {
				renderTo:"gridContainer",
				height:500,
				rowNumber:true,
				columns:[
					// {title:"",dataIndex:"ranking",width:30,formater:rankingFormatter},
					{title:"",dataIndex:"photo",width:40,formater:photoFormatter},
					{title:clientResource.columnGroupName,dataIndex:"group_name",width:200},
					{title:clientResource.columnGroupCalorie,dataIndex:"calorie",width:150,align:"left",sortable:true,selectable:true,formater:colorieFormatter},
					{title:clientResource.columnGroupMiles,dataIndex:"miles",width:150,sortable:true,selectable:true},
					{title:clientResource.columnGroupSteps,dataIndex:"steps",width:150,sortable:true,selectable:true},
					//{title:"跳数",dataIndex:"jumps",width:125,sortable:true,selectable:true},
					{title:clientResource.columnGroupSleep,dataIndex:"sleeptime",width:125,sortable:true,selectable:true},
					{title:clientResource.columnGroupMember,dataIndex:"membercnt",width:125}
				],
				rowSelectable:"none",
				paggingBar: {
					remote: true,
					pageIndex: 1,
					pageSize: 8
				},
				store:{
					data: {
						list_type:_groupType,
						sortField:"calorie",
						sortDirect:"desc" //asc|desc
					}, 
					url: socialActions.getGroupList,
				}
			}

			var gridEvents = nsbase.controls.grid.setting.eventEnums;
			var grid = new nsbase.controls.grid(gridConfig);
			grid.on(gridEvents.rowClick,function(sender,rowData){
				location.href = nsbase.config.actionUrl.social.groupDetail+"?group_id="+rowData.group_id;
			});

			// var dataList = [
			// 	{'group_membercnt': '2/50', 'photo': 'default_photo',group_jumps:0, 'group_sleeptime': 0, 'group_name': 'ET.Go', 'group_calorie': 0, 'group_type': 3, 'group_miles': 0, 'group_id': 2, 'group_steps': 0},
			// ];
			// grid.showData(dataList);

			function rankingFormatter(column, value, obj){
				var cssName = "";
				if(value == 1){
					cssName = "steady";
				} else if(value == 2){
					cssName = "asc";
				} else {
					cssName = "desc";
				}
				return '<span class="grid_icon '+ cssName +'"></span>';
			}

			function photoFormatter (column,value,obj) {
				var imgSrc = "../static/images/photo.png";
				if(value){
					imgSrc = value;
				}
				return '<img src="'+ imgSrc +'" width="30" height="30" onError="this.src=\'../static/images/photo.png\';"/>';
			}
			function colorieFormatter (column,value,obj) {
				return '<span class="grid_icon calorie"></span>' + value + ' cal';
			}
		}
	}
})();